import requests

from api import log
from api.api_recharge import ApiRecharge
from api.api_register_login import ApiRegisterLogin
from tools import common_assert, html_parser


class TestRecharge:
    # 初始化
    def setup_method(self):
        # 获取session对象
        self.session = requests.session()
        # 获取 ApiRecharge 对象
        self.api = ApiRecharge(self.session)
        # 登录
        ApiRegisterLogin(self.session).api_login()

    # 结束
    def teardown_method(self):
        self.session.close()

    # 充值验证码接口测试方法
    def test01_img_code(self, random=123456, status_code=200):
        try:
        # 调用充值验证码接口
            result = self.api.api_img_code(random)
            log.info("充值验证码接口执行结果为：{}".format(result.status_code))
            common_assert(result, status_code=status_code)
        except Exception as e:
            log.error(e)
            raise

    # 后台充值接口测试方法
    def test02_recharge(self,expect_text="form",expect_text2="OK"):
        try:
            # 调用后台充值接口
            self.api.api_img_code(random=123456)
            result = self.api.api_recharge()
            log.info("后台充值接口执行结果为:{}".format(result.json()))
            common_assert(result, expect_text=expect_text)
            log.info("后台充值接口断言通过！")
            # 三方充值
            data  = html_parser(result)
            result = self.session.post(url=data[0],data=data[1])
            print("三方充值的结果为：",result.text)
            common_assert(result, expect_text=expect_text2)
            log.info("三方充值执行结果为:{}".format(result.text))
        except Exception as e:
            log.error(e)
            raise
